//
//  ChannelDetailViewController.m
//  FNer
//
//  Created by 王展 on 15/9/30.
//  Copyright (c) 2015年 wangzhan. All rights reserved.
//

#import "ChannelDetailViewController.h"
#import "UIImageView+WebCache.h"
//#import "CommunityViewController.h"
#import "DataHandle.h"

@interface ChannelDetailViewController ()

@property(nonatomic,assign)BOOL isDingyue;
@property(nonatomic,retain)Channels *channel;
@property(nonatomic,retain)UILabel *titleLabel;

@end

@implementation ChannelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    self.titleLabel.text = @"频道详情";
    self.titleLabel.font = [UIFont systemFontOfSize:18 * XWIDTH];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.titleLabel;
    
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"nighting"];
    if ([string isEqualToString:@"yes"]) {
#warning 改的颜色
        self.titleLabel.textColor = [UIColor blackColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColoring:) name:@"changeColoring" object:nil];
    
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    
    UIView *bigView = [[UIView alloc] initWithFrame:CGRectMake(10 * XWIDTH, 10 * XHIGHT, WIDTH - 10 * XWIDTH * 2, 450 * XHIGHT)];
    bigView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bigView];
    
    UIImageView *titleView = [[UIImageView alloc] initWithFrame:CGRectMake(130 * XWIDTH, 40 * XHIGHT, 90 * XWIDTH, 90 * XHIGHT)];
//    titleView.backgroundColor = [UIColor lightGrayColor];
    [bigView addSubview:titleView];
    [titleView sd_setImageWithURL:[NSURL URLWithString:self.aChannel.channelIcon]];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(50 * XWIDTH, 150 * XHIGHT, 250 * XWIDTH, 25 * XHIGHT)];
//    title.backgroundColor = [UIColor lightGrayColor];
    [bigView addSubview:title];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = self.aChannel.channelName;
    title.textColor = [UIColor lightGrayColor];
    title.font = [UIFont systemFontOfSize:17 * XWIDTH];
    
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * XWIDTH, 185 * XHIGHT , 250 * XWIDTH, 100 * XHIGHT)];
//    contentLabel.backgroundColor = [UIColor lightGrayColor];
    [bigView addSubview:contentLabel];
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = [UIColor lightGrayColor];
    contentLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
    contentLabel.text = self.aChannel.channelDesc;
    [contentLabel sizeToFit];
    
    UIButton *dingyueButton = [[UIButton alloc] initWithFrame:CGRectMake(10 * XWIDTH, 470 * XHIGHT, WIDTH - 2 * 10 * XWIDTH, 50 * XHIGHT)];
    dingyueButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:dingyueButton];
    
    NSMutableDictionary *dic = [[DataHandle shareDataHandle] selectChannel];
    self.channel = [dic objectForKey:self.aChannel.channelName];
    if (self.channel) {
        [dingyueButton setTitle:@"取消订阅" forState:UIControlStateNormal];
        dingyueButton.titleLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
        self.isDingyue = YES;
    }
    else
    {
        [dingyueButton setTitle:@"订阅" forState:UIControlStateNormal];
        dingyueButton.titleLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
        self.isDingyue = NO;
    }

    [dingyueButton setTitleColor:[UIColor lightGrayColor] forState:0];
    [dingyueButton addTarget:self action:@selector(dingyueButtonAction:) forControlEvents:UIControlEventTouchUpInside];

}

//订阅按钮
-(void)dingyueButtonAction:(UIButton *)button
{
    
    if (self.isDingyue == YES) {
        [[DataHandle shareDataHandle] deleteChannel:self.aChannel.channelName];
        [button setTitle:@"订阅" forState:0];
        button.titleLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
        self.isDingyue = NO;
    }
    else
    {
        [[DataHandle shareDataHandle] insertFilmTable:self.aChannel];
        [button setTitle:@"取消订阅" forState:0];
        button.titleLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
        self.isDingyue = YES;
    }
}







-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)changeColoring:(NSNotification *)notification
{
#warning 改的颜色
    self.titleLabel.textColor = [UIColor blackColor];
    
    NSString *string = [notification object];
    if ([string isEqualToString:@"no"]) {
        
#warning 原有颜色
        self.titleLabel.textColor = [UIColor whiteColor];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"nighting"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
